﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;

namespace Basic4Net.OAWeb.Module_Process
{
    public partial class main : BasePage
    {
        private static Amount _amount;
        protected static Amount HAmount
        {
            get {  return _amount ?? (_amount = new Amount()); }
        }

        private t_Amount currentAmount;
        protected void Page_Load(object sender, EventArgs e)
        {
            currentAmount = HAmount.GetActiveAmount(SchoolMark);
            if (currentAmount != null)
            {
                labCurrent.Text = String.Format("{0}年{1}月", currentAmount.FYear, currentAmount.FMonth);
                if(currentAmount.HolType == 1)
                    chkHol.Checked = true;
            }
            else
                labCurrent.Text = "无";

            BindDate();
        }

        protected void BindDate()
        { 
            DateTime dt = DateTime.Now;
            if (currentAmount == null)
            {
                for (int i = 0; i < 24; i++)
                {
                    drDate.Items.Add(new ListItem(String.Format("{0}年{1}月", dt.Year, dt.Month),
                        String.Format("{0}-{1}", dt.Year, dt.Month)));
                    dt = dt.AddMonths(-1);
                }
            }
            else
            {
                while (true)
                {
                    // update by lxy 20100916 添加重复判断 修改bug35
                    if (!drDate.Items.Contains(new ListItem(String.Format("{0}年{1}月", dt.Year, dt.Month),
                        String.Format("{0}-{1}", dt.Year, dt.Month))))
                    {

                        drDate.Items.Add(new ListItem(String.Format("{0}年{1}月", dt.Year, dt.Month),
                                                      String.Format("{0}-{1}", dt.Year, dt.Month)));
                      
                    } 
                    if (dt.Year == currentAmount.FYear && dt.Month == currentAmount.FMonth)
                        break;
                    dt = dt.AddMonths(-1);
                }
                drDate.SelectedValue = String.Format("{0}-{1}", currentAmount.FYear, currentAmount.FMonth);
            }
        }

        protected void btnOKClick(object sender, EventArgs e)
        {
            //currentAmount = HAmount.GetActiveAmount(SchoolMark);
            //if (currentAmount != null)
            //    labCurrent.Text = String.Format("{0}年{1}月", currentAmount.FYear, currentAmount.FMonth);
            //else
            //    labCurrent.Text = "无";
            currentAmount = HAmount.GetActiveAmount(SchoolMark);
            if (currentAmount != null)
            {
                labCurrent.Text = String.Format("{0}年{1}月", currentAmount.FYear, currentAmount.FMonth);
                if (currentAmount.HolType == 1)
                    chkHol.Checked = true;
            }
            else
                labCurrent.Text = "无";

            BindDate();
        }

        [System.Web.Services.WebMethod]
        public static string CreateOrUpdate(string strDate, int isHol)
        {
            string strmsg = String.Empty;
            string[] datearr = strDate.Split(new char[] { '-' });
            if (datearr.Length != 2)
                return strmsg;

            int year = 0;
            int month = 0;
            Int32.TryParse(datearr[0], out year);
            Int32.TryParse(datearr[1], out month);

            HAmount.CreateOrUpdate(year, month, isHol, SchoolMark);
            return strmsg;
        }
    }
}